package com.svse.designPattern.structure.flyweight.flyweightfactory;

import com.svse.designPattern.structure.flyweight.concreteflyweight.ConcreteFlyweight;
import com.svse.designPattern.structure.flyweight.flyweight.Flyweight;

import java.util.HashMap;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/8/2 9:24
 */
public class FlyweightFactory {
    private HashMap<String, Flyweight> flyweights=new HashMap<String, Flyweight>(32);
    public Flyweight getFlyweight(String key){
        Flyweight flyweight=(Flyweight)flyweights.get(key);
        if(flyweight != null){
            System.out.println("具体享元"+key+"已经存在，被成功获取！");
        }else{
            flyweight=new ConcreteFlyweight(key);
            flyweights.put(key, flyweight);
        }
        return  flyweight;
    }
}
